package com.ruoyi.finance.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.finance.mapper.TVoucherTemplateMapper;
import com.ruoyi.finance.mapper.VoucherDetailMapper;
import com.ruoyi.finance.mapper.VoucherTemplateDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.finance.domain.TVoucherDetail;
import com.ruoyi.finance.mapper.TVoucherMapper;
import com.ruoyi.finance.domain.TVoucher;
import com.ruoyi.finance.service.ITVoucherService;

/**
 * 凭证管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-27
 */
@Service
public class TVoucherServiceImpl implements ITVoucherService 
{
    @Autowired
    private TVoucherMapper tVoucherMapper;

    @Autowired
    VoucherDetailMapper voucherDetailMapper;

    /**
     * 查询凭证管理
     * 
     * @param id 凭证管理主键
     * @return 凭证管理
     */
    @Override
    public TVoucher selectTVoucherById(Long id)
    {
        return tVoucherMapper.selectTVoucherById(id);
    }

    /**
     * 查询凭证管理列表
     * 
     * @param tVoucher 凭证管理
     * @return 凭证管理
     */
    @Override
    public List<TVoucher> selectTVoucherList(TVoucher tVoucher)
    {
        return tVoucherMapper.selectTVoucherList(tVoucher);
    }

    /**
     * 新增凭证管理
     * 
     * @param tVoucher 凭证管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTVoucher(TVoucher tVoucher)
    {
        TVoucher tVouchers = tVoucherMapper.selectVoucherName(tVoucher.getNumber());
        if(StringUtils.isNotNull(tVouchers)){
            throw new RuntimeException("凭证字号重复");
        }
        tVoucher.setAccountingPeriod(DateUtils.getDate());
        tVoucher.setAuditStatus(1L);
        tVoucher.setVoidStatus(1);
        int rows = tVoucherMapper.insert(tVoucher);
        List<TVoucherDetail> tVoucherDetailList = tVoucher.getDetailList();
        if(tVoucherDetailList==null){
            throw new RuntimeException("请将信息添加完整");
        }else {
            for (TVoucherDetail tVoucherDetail : tVoucherDetailList)
            {
                TVoucherDetail vtd = new TVoucherDetail();
                vtd.setVoucherId(tVoucher.getId());
                vtd.setSubjectId(tVoucherDetail.getSubjectId());
                vtd.setAbstractId(tVoucherDetail.getAbstractId());
                vtd.setDebitAmount(tVoucherDetail.getDebitAmount());
                vtd.setCreditAmount(tVoucherDetail.getCreditAmount());
                vtd.setCreateId(tVoucher.getCreateId());
                vtd.setCreateTime(DateUtils.getNowDate());
                voucherDetailMapper.insert(vtd);
            }
        }
        return rows;
    }

    /**
     * 修改凭证管理
     * 
     * @param tVoucher 凭证管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTVoucher(TVoucher tVoucher)
    {
        tVoucher.setUpdateTime(DateUtils.getNowDate());
        tVoucherMapper.deleteTVoucherDetailByVoucherId(tVoucher.getId());
        insertTVoucherDetail(tVoucher);
        return tVoucherMapper.updateTVoucher(tVoucher);
    }

    /**
     * 批量删除凭证管理
     * 
     * @param ids 需要删除的凭证管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTVoucherByIds(Long[] ids)
    {
        tVoucherMapper.deleteTVoucherDetailByVoucherIds(ids);
        return tVoucherMapper.deleteTVoucherByIds(ids);
    }

    /**
     * 删除凭证管理信息
     * 
     * @param id 凭证管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTVoucherById(Long id)
    {
        tVoucherMapper.deleteTVoucherDetailByVoucherId(id);
        return tVoucherMapper.deleteTVoucherById(id);
    }

    /**
     * 新增凭证明细信息
     * 
     * @param tVoucher 凭证管理对象
     */
    public void insertTVoucherDetail(TVoucher tVoucher)
    {
        List<TVoucherDetail> tVoucherDetailList = tVoucher.getDetailList();
        Long id = tVoucher.getId();
        if (StringUtils.isNotNull(tVoucherDetailList))
        {
            List<TVoucherDetail> list = new ArrayList<TVoucherDetail>();
            for (TVoucherDetail tVoucherDetail : tVoucherDetailList)
            {
                tVoucherDetail.setVoucherId(id);
                list.add(tVoucherDetail);
            }
            if (list.size() > 0)
            {
                tVoucherMapper.batchTVoucherDetail(list);
            }
        }
    }
}
